﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;

namespace MvcGraffiti.Models
{
	public class GraffitiRepository
	{
		private MvcGraffitiEntities _db;
		private MvcGraffitiEntities Db
		{
			get
			{
				if(_db == null)
					_db = new MvcGraffitiEntities();

				return _db;
			}
		}

		public IQueryable<Graffiti> All()
		{
			return from g in Db.Graffitis
				   select g;
		}

		public Graffiti GetGraffiti(int id)
		{
			return All().Where(g => g.Id == id).FirstOrDefault();
		}

		public string GetUserName(int id)
		{
			return Db.Sessions.Where(s => s.Id == id).Select(s => s.Name).FirstOrDefault();
		}

		public int CreateSession(string name)
		{
			var session = new Session {Name = name};
			Db.Sessions.AddObject(session);
			Db.SaveChanges();

			return session.Id;
		}
	}
}